package cn.lf.android.lawfirm.view.pickerview;

import java.util.ArrayList;


import android.view.View;

import cn.lf.android.lawfirm.R;

public class WheelOptions implements MyExtendAsyncCallback {
	private View view;
	private WheelView wv_option1;
	private WheelView wv_option2;
	private WheelView wv_option3;

	private ArrayList<String> mOptions1Items;
	private ArrayList<String> mOptions1Items2;
	private ArrayList<String> mOptions1Items3;
	private ArrayList<ArrayList<String>> mOptions2Items;
	private ArrayList<ArrayList<ArrayList<String>>> mOptions3Items;
	public int screenheight;

	/**
	 * @JingYang 实现三级PickerView的异步联动功能
	 */
	private MyExtendListener mMyExtendListener;
	/********************************************/
	/**
	 * @author JingYang 项目中收货地址采用省市区三级列表，如果某一项文字太长，会在滑动过程中产生重叠现象。
	 *         这个WheelView是用StaticLayout来控制text的绘制的，只能自动换行。
	 *         为了避免这个问题只能把文字超过一定长度的内容用“...”截断
	 */
	private boolean isSingleLine;
	private int maxTextItem;
	private int mTextSize;

	/**
	 * 设置是否单行显示
	 * 
	 * @param single
	 */
	public void setSingleLine(boolean single) {
		isSingleLine = single;
	}

	/**
	 * 设置超过多少位的文字采取“...”截断 项目中需要这个功能的只有选择省市县地址，传7就好
	 * 
	 * @param num
	 */
	public void setMaxTextItem(int num) {
		maxTextItem = num;
	}

	/**
	 * 设置文字大小
	 * 
	 * @param size
	 */
	public void setTextSize(int size) {
		mTextSize = size;
	}

	/********************************************/

	public View getView() {
		return view;
	}

	public void setView(View view) {
		this.view = view;
	}

	public WheelOptions(View view) {
		super();
		this.view = view;
		setView(view);
	}

	/**
	 * @JingYang 实现三级PickerView的异步联动功能
	 */
	public void setMyExtendListener(MyExtendListener listener) {
		mMyExtendListener = listener;
	}

	/********************************************/
	public void setPicker(ArrayList<String> options1Items,
			ArrayList<String> options2Items, ArrayList<String> options3Items) {
		this.mOptions1Items = options1Items;
		this.mOptions1Items2 = options2Items;
		this.mOptions1Items3 = options3Items;
		int len = ArrayWheelAdapter.DEFAULT_LENGTH;
		if (this.mOptions3Items == null)
			len = 8;
		if (this.mOptions2Items == null)
			len = 12;
		wv_option1.setSingleLine(isSingleLine);
		wv_option1.setMaxTextItem(maxTextItem);
		wv_option1.setAdapter(new ArrayWheelAdapter(mOptions1Items, len));// 设置显示数据
		wv_option1.setCurrentItem(0);// 初始化时显示的数据
		// 选项2
		wv_option2.setSingleLine(isSingleLine);
		wv_option2.setMaxTextItem(maxTextItem);
		if (mOptions1Items2 != null)
			wv_option2.setAdapter(new ArrayWheelAdapter(mOptions1Items2, len));// 设置显示数据
		wv_option2.setCurrentItem(wv_option1.getCurrentItem());// 初始化时显示的数据

		// 选项3
		wv_option3 = (WheelView) view.findViewById(R.id.options3);

		wv_option3.setSingleLine(isSingleLine);
		wv_option3.setMaxTextItem(maxTextItem);

		if (mOptions1Items3 != null)
			wv_option3.setAdapter(new ArrayWheelAdapter(mOptions1Items3, len));// 设置显示数据
		wv_option3.setCurrentItem(wv_option3.getCurrentItem());// 初始化时显示的数据
	}

	public void setPicker(ArrayList<String> optionsItems) {
		setPicker(optionsItems, null, null, false);
	}

	public void setPicker(ArrayList<String> options1Items,
			ArrayList<ArrayList<String>> options2Items, boolean linkage) {
		setPicker(options1Items, options2Items, null, linkage);
	}

	public void setPicker(ArrayList<String> options1Items,
			ArrayList<ArrayList<String>> options2Items,
			ArrayList<ArrayList<ArrayList<String>>> options3Items,
			boolean linkage) {
		this.mOptions1Items = options1Items;
		this.mOptions2Items = options2Items;
		this.mOptions3Items = options3Items;
		int len = ArrayWheelAdapter.DEFAULT_LENGTH;
		if (this.mOptions3Items == null)
			len = 8;
		if (this.mOptions2Items == null)
			len = 12;
		// 选项1
		wv_option1 = (WheelView) view.findViewById(R.id.options1);

		/**
		 * @author JingYang
		 */
		wv_option1.setSingleLine(isSingleLine);
		wv_option1.setMaxTextItem(maxTextItem);
		/**********************************************/

		wv_option1.setAdapter(new ArrayWheelAdapter(mOptions1Items, len));// 设置显示数据
		wv_option1.setCurrentItem(0);// 初始化时显示的数据
		// 选项2
		wv_option2 = (WheelView) view.findViewById(R.id.options2);

		/**
		 * @author JingYang
		 */
		wv_option2.setSingleLine(isSingleLine);
		wv_option2.setMaxTextItem(maxTextItem);
		/**********************************************/

		if (mOptions2Items != null)
			wv_option2.setAdapter(new ArrayWheelAdapter(mOptions2Items.get(0)));// 设置显示数据
		wv_option2.setCurrentItem(wv_option1.getCurrentItem());// 初始化时显示的数据
		// 选项3
		wv_option3 = (WheelView) view.findViewById(R.id.options3);

		/**
		 * @author JingYang
		 */
		wv_option3.setSingleLine(isSingleLine);
		wv_option3.setMaxTextItem(maxTextItem);
		/**********************************************/

		if (mOptions3Items != null)
			wv_option3.setAdapter(new ArrayWheelAdapter(mOptions3Items.get(0)
					.get(0)));// 设置显示数据
		wv_option3.setCurrentItem(wv_option3.getCurrentItem());// 初始化时显示的数据

		/**
		 * @author JingYang
		 */
		if (mTextSize == 0) {
			// 根据屏幕密度来指定选择器字体的大小(不同屏幕可能不同)
			mTextSize = (screenheight / 130) * 4;
		}
		/**********************************************/

		wv_option1.TEXT_SIZE = mTextSize;
		wv_option2.TEXT_SIZE = mTextSize;
		wv_option3.TEXT_SIZE = mTextSize;

		if (this.mOptions2Items == null)
			wv_option2.setVisibility(View.GONE);
		if (this.mOptions3Items == null)
			wv_option3.setVisibility(View.GONE);

		// 联动监听器
		OnWheelChangedListener wheelListener_option1 = new OnWheelChangedListener() {

			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				if (mMyExtendListener == null) {
					if (mOptions2Items != null) {
						wv_option2
								.setAdapter(new ArrayWheelAdapter(
										mOptions2Items.get(wv_option1
												.getCurrentItem())));
						wv_option2.setCurrentItem(0);
					}
					if (mOptions3Items != null) {
						wv_option3.setAdapter(new ArrayWheelAdapter(
								mOptions3Items.get(wv_option1.getCurrentItem())
										.get(wv_option2.getCurrentItem())));
						wv_option3.setCurrentItem(0);
					}
				}
				/**
				 * @JingYang 实现三级PickerView的异步联动功能 通知外部最左边的list滚动数据发生变化
				 */
				else {
					mMyExtendListener.onWheelChanged(0, newValue);
				}
				/********************************************/
			}
		};
		OnWheelChangedListener wheelListener_option2 = new OnWheelChangedListener() {

			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				if (mMyExtendListener == null) {
					if (mOptions3Items != null) {
						wv_option3.setAdapter(new ArrayWheelAdapter(
								mOptions3Items.get(wv_option1.getCurrentItem())
										.get(wv_option2.getCurrentItem())));
						wv_option3.setCurrentItem(0);
					}
				}
				/**
				 * @JingYang 实现三级PickerView的异步联动功能 通知外部中间的list滚动数据发生变化
				 */
				else {
					mMyExtendListener.onWheelChanged(1, newValue);
				}
				/********************************************/
			}
		};

		// 添加联动监听
		if (options2Items != null && linkage)
			wv_option1.addChangingListener(wheelListener_option1);
		if (options3Items != null && linkage)
			wv_option2.addChangingListener(wheelListener_option2);
	}

	/**
	 * 设置选项的单位
	 * 
	 * @param label1
	 * @param label2
	 * @param label3
	 */
	public void setLabels(String label1, String label2, String label3) {
		if (label1 != null)
			wv_option1.setLabel(label1);
		if (label2 != null)
			wv_option2.setLabel(label2);
		if (label3 != null)
			wv_option3.setLabel(label3);
	}

	/**
	 * 设置是否循环滚动
	 * 
	 * @param cyclic
	 */
	public void setCyclic(boolean cyclic) {
		wv_option1.setCyclic(cyclic);
		wv_option2.setCyclic(cyclic);
		wv_option3.setCyclic(cyclic);
	}

	/**
	 * 返回当前选中的结果对应的位置数组 因为支持三级联动效果，分三个级别索引，0，1，2
	 * 
	 * @return
	 */
	public int[] getCurrentItems() {
		int[] currentItems = new int[3];
		currentItems[0] = wv_option1.getCurrentItem();
		currentItems[1] = wv_option2.getCurrentItem();
		currentItems[2] = wv_option3.getCurrentItem();
		return currentItems;
	}

	public void setCurrentItems(int option1, int option2, int option3) {
		wv_option1.setCurrentItem(option1);
		wv_option2.setCurrentItem(option2);
		wv_option3.setCurrentItem(option3);
	}

	/**
	 * @JingYang 实现三级PickerView的异步联动功能 外部异步加载数据完成后刷新list
	 */
	@Override
	public void onDataReceived(int wheelIndex, ArrayList<String> list) {
		if (wheelIndex == 1) {// 左边的list数据变化，中间的list数据更新
			wv_option2.setAdapter(new ArrayWheelAdapter(list));
			wv_option2.setCurrentItem(0);
		} else if (wheelIndex == 2) {// 中间的list数据变化，右边的list数据更新
			wv_option3.setAdapter(new ArrayWheelAdapter(list));
			wv_option3.setCurrentItem(0);
		}
	}
	/********************************************/
}
